package com.yskj.mwk.controller;

import com.yskj.mwk.service.UploadService;
import com.yskj.mwk.utils.JsonResult;
import com.yskj.mwk.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Base64;

@RestController
@RequestMapping("upload")
public class UploadController {
    @Autowired
    private UploadService uploadService;

    /**
     * 头像上传
     * */
    @PostMapping("/uploadImage")
    public JsonResult uploadImage(HttpServletRequest request, @RequestParam("file") MultipartFile file){
        //需要对接腾讯查看图片是否合法

        if (file == null) {
            return  ResultUtils.failure(201,"文件错误");
        }

        String picUrl = uploadService.uploadAvatarImage(request.getRequestURL().toString(), file);
        if (picUrl == null) {
            return ResultUtils.failure(101,"请上传合法图片");
        }else{
            return ResultUtils.success("上传成功",picUrl);
        }
    }

    /**
     * 用户合成图片上传的图片
     * */
    @PostMapping("/uploadCommonImage")
    public JsonResult uploadCommonImage(HttpServletRequest request, @RequestParam("file") MultipartFile file){

        if (file == null) {
            return  ResultUtils.failure(201,"文件错误");
        }

        String picUrl = uploadService.uploadCommonImage(request.getRequestURL().toString(), file);
        return ResultUtils.success("上传成功",picUrl);
    }

    /**
     * 最终结果图片
     * */
    @PostMapping("/uploadBase64")
    public JsonResult uploadBase64Image(HttpServletRequest request,@RequestBody String base64Image) {
        String base64Data = base64Image.split(",")[1];

            String filePath=uploadService.uploadBase64(base64Data,request.getRequestURL().toString());

            return ResultUtils.success("上传成功",filePath);

    }

    /**
     * 最终结果图片
     * */
    @PostMapping("/uploadResultImage")
    public JsonResult uploadResultImage(HttpServletRequest request, @RequestParam("file") MultipartFile file){

        if (file == null) {
            return  ResultUtils.failure(201,"文件错误");
        }

        String picUrl = uploadService.uploadResultImage(request.getRequestURL().toString(), file);
        return ResultUtils.success("上传成功",picUrl);
    }

    /**
     * 商品图
     * */
    @PostMapping("/uploadGoodsImage")
    public JsonResult uploadGoodsImage(HttpServletRequest request, @RequestParam("file") MultipartFile file){

        if (file == null) {
            return  ResultUtils.failure(201,"文件错误");
        }

        String picUrl = uploadService.uploadGoodsImage(request.getRequestURL().toString(), file);
        return ResultUtils.success("上传成功",picUrl);
    }

    /**
     * 抠图
     * */
    @PostMapping("/txMatting")
    public JsonResult txMatting(HttpServletRequest request, @RequestParam("picUrl") String picUrl){

        String resultUrl = uploadService.txMatting(request.getRequestURL().toString(), picUrl);
        if (resultUrl == null) {
            return  ResultUtils.failure(101,"抠图失败");
        }else{
            return ResultUtils.success("上传成功",resultUrl);
        }

    }
}
